<?php
/**
 * @Description 
 * @Sonkwl Xiong
 * @Date 2024/04/18 13:49:35
 * @GET 
 * 
 * @POST 
 * 
 * @Response 
 *  
 */
include_once __DIR__."/config.php";
class index{

    public static function Run(){
        call_user_func("index::".$_GET["p"]."_".$_SERVER["REQUEST_METHOD"]);
    }

    public static function GetPDO(){
        return new PDO(PDOstr,USER,PWD,array(PDO::ATTR_EMULATE_PREPARES => false));
    }

    public static function list_GET(){
        $db=self::GetPDO();
        $page=$_GET["page"];
        $dbh=$db->prepare("select * from project_system_t where i_drop=0 order by sid desc limit ?,10;");
        $dbh->execute([$page*10-10]);
        $list=$dbh->fetchAll(PDO::FETCH_ASSOC);

        $dbh=$db->prepare("select count(*) from project_system_t where i_drop=0;");
        $dbh->execute();
        $rs=$dbh->fetchAll(PDO::FETCH_NUM);
        $rows=$rs[0][0];
        $pages=(int)($rows/10)+$rows%10?1:0;
        
        echo json_encode([
            "res"=>"OK",
            "list"=>$list,
            "rows"=>$rows,
            "pages"=>$pages,
            "page"=>$page
        ]);
    }

    public static function add_POST(){
        $db=self::GetPDO();
        $dbh=$db->prepare("insert into project_system_t(s_project_name,s_project_path,s_status,s_keypart,d_time,i_drop) values(?,?,?,?,now(),0);");
        $dbh->execute([
            $_POST["s_project_name"],
            $_POST["s_project_path"],
            "规划",
            ""
        ]);
        echo json_encode([
            "res"=>"OK"
        ]);
    }

    public static function edit_POST(){
        $db=self::GetPDO();
        $dbh=$db->prepare("update project_system_t set s_project_name=?,s_project_path=?,d_time=now() where sid=?;");
        $dbh->execute([
            $_POST["s_project_name"],
            $_POST["s_project_path"],
            $_POST["sid"]
        ]);
        echo json_encode([
            "res"=>"OK"
        ]);
    }

    public static function delete_POST(){
        $db=self::GetPDO();
        $dbh=$db->prepare("update project_system_t set d_time=now(),i_drop=1 where sid=?;");
        $dbh->execute([
            $_POST["sid"]
        ]);
        echo json_encode([
            "res"=>"OK"
        ]);
    }
}

index::Run();
?>